<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增信息</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="x-body">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <input id='hiddenId' type='hidden' th:value='${sysFile.id}' name='id'/>
                <input th:value='${sysFile.pathUrl}' class='layui-input' type='hidden' name='pathUrl' id='pathUrl'>
                <input th:value='${sysFile.fileName}' class='layui-input' type='hidden' name='fileName' id='fileName'>
                <input th:value='${sysFile.sizeValue}' class='layui-input' type='hidden' name='sizeValue'
                       id='sizeValue'>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span class='x-red'>*</span>文件名</label>
                    <div class='layui-input-inline'>
                        <input th:value='${sysFile.title}' class='layui-input' placeholder='请输入文件名' type='text'
                               name='title' id='titleName' lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'></label>
                    <div class="layui-input-inline">
                        <div class="layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal" id="editFile"><i
                                    class="layui-icon">&#xe67c;</i>上传文件
                            </button>
                        </div>
                        <div class="layui-upload-list">
                            <p id="fileN"></p>
                        </div>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label for='visitUrl' class='layui-form-label'><span class='x-red'>*</span>访问地址</label>
                    <div class='layui-input-inline'>
                        <input th:value='${sysFile.visitUrl}' class='layui-input' type='text'
                               name='visitUrl' id='visitUrl' lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span class='x-red'>*</span>状态</label>
                    <div class='layui-input-inline'>
                        <select id="selectType" name="status" lay-filter="status" th:value="${sysFile.status}">
                            <option value="1">有效</option>
                            <option value="0">无效</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <button class="layui-btn layui-btn-danger" lay-filter="add" lay-submit="">
                        <i class="iconfont">&#xe6ad;</i>立即提交
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    layui.use(['form', 'layer', 'upload', 'element'], function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload;
        //编辑获取文件名
        var fileName = $('#fileName').val();
        $("#fileN").html(fileName);
        var loading = null;
        //文件上传
        upload.render({
            elem: '#editFile',
            url: '/fileUpload/upload',
            accept: 'file',
            exts: 'zip|rar|7z|doc|docx|pdf|txt|xls|ppt|xlsx|pptx|img|jpg|png|gif|bmp|jpeg',
            size: 1024 * 10,
            before: function (obj) {
                obj.preview(function (index, file, result) {
                    $("#fileN").html(file.name);
                    $("#sizeValue").val((file.size / 1024).toFixed(2) + "KB");
                    loading = layer.load();
                });
            },
            done: function (res) {
                if (res.code == 0) {
                    layer.close(loading);
                    layer.msg("上传成功");
                    $("#fileName").val(res.data.fileName);
                    $("#pathUrl").val(res.data.src);
                } else {
                    layer.close(loading);
                    $("#fileN").html();
                    layer.msg(res.msg);
                }
            },
            error: function () {
                layer.close(loading);
                layer.msg("上传接口异常");
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            $.ajax({
                url: "/file/add",
                type: "POST",
                data: data.field,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 500 || result.code == 5000100 || result.code == 5000101 || result.code == 5000102) {
                        layer.alert(result.msg);
                    } else {
                        layer.alert("设置成功", {icon: 6}, function () {
                            xadmin.close();
                            xadmin.father_reload();
                        });
                    }
                }
            });
            return false;
        });
        initSelectType();
    });
</script>
</body>
</html>
